//
//  LGProtocolAlertView.m
//  HeartDiseaseManage
//
//  Created by 1234 on 2023/2/13.
//

#import "LGProtocolAlertView.h"

@interface LGProtocolAlertView()<UITextViewDelegate>
/**   */
@property (nonatomic , strong)UIView *baseView;
/**   */
@property (nonatomic , strong)UILabel *titleLabel;
/**   */
@property (nonatomic , strong)UITextView *contentTF;
/**   */
@property (nonatomic , strong)UIView *lineView_H;
/**   */
@property (nonatomic , strong)UIView *lineView_V;
/**   */
@property (nonatomic , strong)UIButton *cancelBtn;
/**   */
@property (nonatomic , strong)UIButton *sureBtn;

@end

@implementation LGProtocolAlertView

- (instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        [self createSubView];
        [self showAgreementContent];
        [self showAnimation];
    }
    return self;
}


-(void)showAnimation{
    [UIView animateWithDuration:0.4 animations:^{
        self.backgroundColor = [UIColor colorWithString:@"#0D0D0D" alpha:0.7];
        self.baseView.transform = CGAffineTransformScale(self.baseView.transform, 1000, 1000);
    }];
}


//拒绝
-(void)refuseBtnAction{
    [UIView animateWithDuration:0.4 animations:^{
        self.backgroundColor = [UIColor colorWithString:@"#0D0D0D" alpha:0];
        self.baseView.transform = CGAffineTransformScale(self.baseView.transform, 0.001, 0.001);
    } completion:^(BOOL finished) {
        [self removeFromSuperview];
    }];
}

//同意
-(void)agreeBtnAction{
    [[NSUserDefaults standardUserDefaults] setObject:@"Y" forKey:@"agreement"];
    [self refuseBtnAction];
    if (self.agreementAlertAction) {
        self.agreementAlertAction(@"continue");
    }
}


-(BOOL)textView:(UITextView *)textView shouldInteractWithURL:(NSURL *)URL inRange:(NSRange)characterRange interaction:(UITextItemInteraction)interaction{
    if (self.agreementAlertAction) {
        if([URL isEqual:[NSURL URLWithString:@"content://agreement"]]){
            [self refuseBtnAction];
            self.agreementAlertAction(@"agreement");
            
        }else if ([URL isEqual:[NSURL URLWithString:@"content://privacy"]]){
            [self refuseBtnAction];
            self.agreementAlertAction(@"privacy");
            
        }
    }
    NSLog(@">>???>>>%@",URL);
    return YES;
}

-(void)showAgreementContent{
    NSString *content = @"为了更好地保障您的合法权益，请您阅读并同意以下协议《用户协议》和《隐私政策》。";
    NSMutableAttributedString *attStr = [[NSMutableAttributedString alloc]initWithString:content];
    NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
    [paragraphStyle setLineSpacing:5];
    [attStr addAttributes:@{NSParagraphStyleAttributeName:paragraphStyle,NSFontAttributeName:LGFont(15)} range:NSMakeRange(0, content.length)];
    NSRange range1 = [content rangeOfString:@"《用户协议》"];
    NSRange range2 = [content rangeOfString:@"《隐私政策》"];
    [attStr addAttribute:NSLinkAttributeName value:@"content://agreement" range:range1];
    [attStr addAttribute:NSLinkAttributeName value:@"content://privacy" range:range2];
    self.contentTF.attributedText = attStr;
    self.contentTF.linkTextAttributes = @{NSForegroundColorAttributeName:[UIColor colorWithString:@"#429BFF"],NSFontAttributeName:LGFontWeight(16, UIFontWeightMedium)};
}

#pragma mark ====> 创建控件
-(void)createSubView{
    self.backgroundColor = [UIColor colorWithString:@"#0D0D0D" alpha:0];
    [self addSubview:self.baseView];
    [self.baseView addSubview:self.titleLabel];
    [self.baseView addSubview:self.contentTF];
    [self.baseView addSubview:self.lineView_H];
    [self.baseView addSubview:self.lineView_V];
    [self.baseView addSubview:self.cancelBtn];
    [self.baseView addSubview:self.sureBtn];
    [self.baseView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.centerY.equalTo(self);
        make.width.equalTo(@(viewPix(320)));
        make.height.equalTo(@(viewPix(240)));
    }];
    [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.baseView).offset(viewPix(30));
        make.left.equalTo(self.baseView).offset(viewPix(24));
        make.right.equalTo(self.baseView).offset(-viewPix(24));
        make.height.equalTo(@(viewPix(24)));
    }];
    [self.contentTF mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.titleLabel.mas_bottom).offset(viewPix(15));
        make.left.right.equalTo(self.titleLabel);
        make.bottom.equalTo(self.lineView_H.mas_top).offset(-viewPix(25));
    }];
    
    [self.lineView_H mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(self.baseView);
        make.bottom.equalTo(self.baseView).offset(-viewPix(55));
        make.height.equalTo(@(1));
    }];
    
    [self.lineView_V mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.lineView_H.mas_bottom);
        make.bottom.equalTo(self.baseView);
        make.centerX.equalTo(self.baseView);
        make.width.equalTo(@(1));
    }];
    [self.cancelBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.lineView_H.mas_bottom);
        make.right.equalTo(self.lineView_V.mas_left);
        make.left.bottom.equalTo(self.baseView);
    }];
    [self.sureBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.lineView_V.mas_right);
        make.right.bottom.equalTo(self.baseView);
        make.top.equalTo(self.cancelBtn);
    }];
}
-(UIView *)baseView{
    if(!_baseView){
        _baseView = [[UIView alloc]init];//WithFrame:CGRectMake((Screen_W-viewPix(290))/2.0, (Screen_H-viewPix(154))/2.0, viewPix(290), viewPix(154))
        _baseView.transform = CGAffineTransformScale(_baseView.transform, 0.001, 0.001);
        _baseView.backgroundColor = [UIColor whiteColor];
        _baseView.cornerRidus = 20;
    }
    return _baseView;
}

-(UILabel *)titleLabel{
    if(!_titleLabel){
        _titleLabel = [UILabel lableWithText:@"服务协议及隐私保护" colorString:@"#333333" textFont:LGFontWeight(18, UIFontWeightMedium) textAlignment:NSTextAlignmentCenter lines:1];
    }
    return _titleLabel;
}

-(UITextView *)contentTF{
    if(!_contentTF){
        _contentTF = [[UITextView alloc]init];
        _contentTF.textAlignment = NSTextAlignmentLeft;
        _contentTF.scrollEnabled = NO;
        _contentTF.font = LGFont(17);
        _contentTF.editable = NO;
        _contentTF.delegate = self;
    }
    return _contentTF;
}

-(UIView *)lineView_H{
    if(!_lineView_H){
        _lineView_H = [[UIView alloc]init];
        _lineView_H.backgroundColor = [UIColor colorWithString:@"#E5E5E5"];
    }
    return _lineView_H;
}

-(UIView *)lineView_V{
    if(!_lineView_V){
        _lineView_V = [[UIView alloc]init];
        _lineView_V.backgroundColor = [UIColor colorWithString:@"#E5E5E5"];
    }
    return _lineView_V;
}

-(UIButton *)cancelBtn{
    if(!_cancelBtn){
        _cancelBtn = [UIButton buttonWithTitle:@"不同意" titleFont:LGFont(17) textColor:@"#333333" imageName:@""];
        [_cancelBtn addTarget:self action:@selector(refuseBtnAction) forControlEvents:UIControlEventTouchUpInside];
    }
    return _cancelBtn;
}

-(UIButton *)sureBtn{
    if(!_sureBtn){
        _sureBtn = [UIButton buttonWithTitle:@"同意" titleFont:LGFontWeight(17, UIFontWeightMedium) textColor:@"#2E8FFF" imageName:@""];
        [_sureBtn addTarget:self action:@selector(agreeBtnAction) forControlEvents:UIControlEventTouchUpInside];
    }
    return _sureBtn;
}





@end
